// pages/pie/index.js

var app = getApp();
var hostUrl = require("../../host.js");
var Charts = require('wxcharts.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    project_name:"",
    company_name:"",
    avatarUrl: '',
    nickName: '',
    shareholders_distribution_important_list:{},
    shareholders_distribution_list:[],
    record_code: "",             //报告记录code
    shareholders_amount:0,      //股东数量
    text:"报告记录",
    have_calculation:1,

    color: ["#7bb5d5", "#feb45f", "#b2df62", "#b8bae0","#9ccfc8"]
  },

  onShareAppMessage: function (res) {
    // console.log(res);
    var data = {
      title: "科学的工具助您合理分配股权",
      imageUrl: "../../image/fenxiang.jpeg",
      path: "/pages/index/index",
    }
    return data;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      avatarUrl: JSON.parse(wx.getStorageSync('userInfo')).avatarUrl,
      nickName: JSON.parse(wx.getStorageSync('userInfo')).nickName,
    })

    var url = hostUrl.report_info;
    var data = { 
      record_id: options.record_id,
      utoken: wx.getStorageSync("utoken"),
    };
    var method = 'POST';  
    var params = app.paramsMd5(data);
    var that = this;
    //请求服务器
    wx.request({
      url: url,
      data: params,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      success: function (response) {

        if (response.data.status) {
           
          var shareholders_distribution_list = response.data.msg.shareholders_distribution_list;

          that.setData({
            shareholders_distribution_list: response.data.msg.shareholders_distribution_list,
            shareholders_distribution_important_list :response.data.msg.shareholders_distribution_important_list[0],
            company_name : response.data.msg.company_name,
            record_code : response.data.msg.record_code,
            shareholders_amount : response.data.msg.shareholders_amount,
            project_name: response.data.msg.project_name
          });
          var option = {
            canvasId: 'pieCanvas',
            type: 'pie',
            yAxis: {
              min: 1
            }, series: [
              ],
            legend:false,
            width: 320,
            height: 300,
          };
          var series = [];
          for (var kid in shareholders_distribution_list){
            series.push({
              name: "股东"+shareholders_distribution_list[kid].shareholders_name,
              data: Number(shareholders_distribution_list[kid].shareholders_describe),
              color: that.data.color[kid]
            });
          };
          
          if (series.length > 0){
            option.series = series;
            new Charts(option);
          }
         
        }else{

          wx.showToast({
            title: response.data.msg,
            image:"",
          })
        }
      }
    });

    app.globalData.have_calculation = 1;
    app.cleraGlobalData();
  },

  icon_redirectTo: function () {
    wx.redirectTo({
      url: '/pages/history/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

});
